import React, { Component, PropTypes } from 'react';
import { connect } from 'dva';
import dataEditCreate from '../DataEdit'
class Step2 extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
        };
    }
    getFormData() {
        const form = this.refs.form2.getForm()
        let formData = null;
        form.validateFields((err, values) => {
            if (!err) {
                formData = values;
            }
        })
        return formData
    }
    handleChange() {
        const daoName = this.props.daoName
        this.props.dispatch({
            type: `${daoName}_DataImport/setDataMapsChange`,
            payload: { dataMapsChange: true }
        });
    }
    render() {
        let items = []
        const { columns, dataMaps, daoName, visible, controls, modelName } = this.props
        if (columns.length === 0) {
            return null
        }
        if (!dataMaps) {
            this.initDataMap()
        }
        let formData2 = {}
        if (dataMaps && dataMaps.length > 0) {
            dataMaps.map((d, i) => {
                formData2[d.DestColName] = !d.SrcColIndex || d.SrcColIndex === -1 ? (d.SrcColName ? d.SrcColName : "") : String(d.SrcColIndex)
            })
        }
        let grid = 24 / 24
        if (controls.length > 5) {
            grid = 12 / 24
        }
        controls.forEach((c, i) => {
            let isParams = false;
            this.props.paramNames.map((p, i) => {
                if (p === c.name) {
                    isParams = true;
                }
            })
            let datas = []
            if (isParams) {
                datas.push({ value: "@" + c.name, label: c.label + "参数" })
            } else {
                for (let i = 0; i < columns.length; i++) {
                    datas.push({ value: String(i), label: columns[i] })
                }
                if (c.ignore) {
                    datas.push({ value: "-2", label: "忽略" })
                }
            }
            const selectMap = { type: "select", grid: grid, required: true, data: datas, name: c.name, label: c.label, onChange: this.handleChange.bind(this) }
            items.push(selectMap)
        })
        const EditForm = dataEditCreate(modelName);
        return (
            <div style={{ display: visible ? "block" : "none" }}>
                <EditForm ref="form2" modelName={modelName} daoName={daoName} controls={items} record={formData2} selectFirst={false} />
            </div>)
    }
}
Step2.propTypes = {
    modelName: PropTypes.string,
    daoName: PropTypes.string,
    controls: PropTypes.array,
    paramNames: PropTypes.array,
    visible: PropTypes.bool
};
Step2.defaultProps = {
    daoName: "",
    controls: [],
    paramNames: [],
    visible: false
};
export default function step2Create(daoName) {
    function mapStateToProps(state, props) {
        const modelName = props.modelName ? props.modelName : daoName
        const { dataMaps, isNewMap, columns } = state[`${daoName}_DataImport`];
        return {
            dataMaps, isNewMap, columns, modelName
        }
    }
    return connect(mapStateToProps, null, null, { withRef: true })(Step2);
}
